﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;

namespace ERPStore.NewsLetters.Repositories
{
	public class NewsLettersDbContext : DbContext
	{
		public NewsLettersDbContext()
			: this("name = ERPStoreConnectionString")
		{

		}

		public NewsLettersDbContext(string connectionString)
			: base(connectionString)
		{
			var csBuilder = new SqlConnectionStringBuilder(this.Database.Connection.ConnectionString);
			csBuilder.ApplicationName = this.GetType().Assembly.FullName + "," + System.Environment.MachineName;
			this.Database.Connection.ConnectionString = csBuilder.ConnectionString;

			Configuration.LazyLoadingEnabled = false;
			Configuration.ProxyCreationEnabled = false;
		}

		protected override void OnModelCreating(DbModelBuilder modelBuilder)
		{
			modelBuilder.Configurations.Add(new Mapping.TrackMap());
			modelBuilder.Configurations.Add(new Mapping.EmailSentMap());
			modelBuilder.Configurations.Add(new Mapping.UnsubscriptionMap());
			modelBuilder.Configurations.Add(new Mapping.EmailRecipientMap());
			modelBuilder.Configurations.Add(new Mapping.PendingItemMap());
			modelBuilder.Configurations.Add(new Mapping.EmailListSourceMap());
			modelBuilder.Configurations.Add(new Mapping.SMTPServerMap());
			modelBuilder.Configurations.Add(new Mapping.CampaignMap());
			modelBuilder.Configurations.Add(new Mapping.NewsLetterMap());
			modelBuilder.Configurations.Add(new Mapping.CampaignStatMap());
			modelBuilder.Configurations.Add(new Mapping.EmailRecipientByEmailListSourceMap());
			modelBuilder.Configurations.Add(new Mapping.UserMap());
			modelBuilder.Configurations.Add(new Mapping.EntitySelectionItemMap());
		}
	}
}
